var setting = {
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		onClick: onClick,
		onRightClick: OnRightClick
	}
};

function onClick(event, treeId, treeNode, clickFlag) {
	$$("po.deptId").value= treeNode.id;
	$$("deptName").innerHTML=treeNode.name;
	if(treeNode.id==0)
	return;
	list(1,treeNode.id);
}	
var zTree, rMenu;
function treeInit()
{
	var url = getDeptAction()+"?cmd=goTree";
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(returnedData){
			$.fn.zTree.init($("#tree"), setting, returnedData);
			zTree = $.fn.zTree.getZTreeObj("tree");
			rMenu = $("#rMenu");
		}
	});
	
}

function OnRightClick(event, treeId, treeNode) {
	if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
		zTree.cancelSelectedNode();
		showRMenu("root", event.clientX, event.clientY);
	} else if (treeNode && !treeNode.noR) {
		zTree.selectNode(treeNode);
		showRMenu("node", event.clientX, event.clientY);
	}
}

function showRMenu(type, x, y) {
	$("#rMenu ul").show();
	if (type=="root") {
		$("#m_del").hide();
		$("#m_add").hide();
		$("#m_modify").hide();
	} else {
		$("#m_del").show();
		$("#m_add").show();
		$("#m_modify").show();
	}
	rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

	$("body").bind("mousedown", onBodyMouseDown);
}
function hideRMenu() {
	if (rMenu) rMenu.css({"visibility": "hidden"});
	$("body").unbind("mousedown", onBodyMouseDown);
}

function hideParentRMenu(pw){
	if (pw.rMenu) pw.rMenu.css({"visibility": "hidden"});
	pw.$("body").unbind("mousedown", function(event){
		if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
			pw.rMenu.css({"visibility" : "hidden"});
		}
	});
}

function onBodyMouseDown(event){
	if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
		rMenu.css({"visibility" : "hidden"});
	}
}
var addCount = 1;
function addTreeNode() {
	hideRMenu();
	var newNode = { name:"增加" + (addCount++)};
	if (zTree.getSelectedNodes()[0]) {
		newNode.checked = zTree.getSelectedNodes()[0].checked;
		zTree.addNodes(zTree.getSelectedNodes()[0], newNode);
	} else {
		zTree.addNodes(null, newNode);
	}
}
function removeTreeNode() {
	hideRMenu();
	var nodes = zTree.getSelectedNodes();
	if (nodes && nodes.length>0) {
		if (nodes[0].children && nodes[0].children.length > 0) {
			var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
			if (confirm(msg)==true){
				zTree.removeNode(nodes[0]);
			}
		} else {
			zTree.removeNode(nodes[0]);
		}
	}
}






function goAddDept()
{
	var id = "";
	if (zTree.getSelectedNodes()[0]) {
		id = zTree.getSelectedNodes()[0].id;
	}else{
		id = "0";
	}
	var retValue = window.showModalDialog(getDeptAction()
			+ "?cmd=goAdd&po.id=" + id,
			window, "menubar=no;toolbar=no");
}

function checkForm(){
	var success = true;
	if( !checkBlank($$("po.deptName"),"部门名称") || !checkCharNumber($$("po.deptName"),"部门名称") ||!MaxLength($$("po.deptName"),"部门名称",30))
	{
		success = false;
	}
	else if(!checkBlank($$("po.deptCname"),"部门中文名称")||!MaxLength($$("po.deptCname"),"部门中文名称",30))
	{
		success = false;
	}
	else if(!checkTelephone($$("po.telephone"),"电话"))
	{
		success = false;
	}
	else if(!checkMail($$("po.email")))
	{
		success = false;
	}
	return success;
}

function getDeptAction(){
 return "dept.action";
}

function getDeptForm(){
return $("#deptForm");
}

/**
 * 添加一个部门
 */
function addDept() {
	if(!checkForm())
	{
		return;
	}	
	var url = getDeptAction()+"?"+getDeptForm().formSerialize();	
	new $.ajax({url:url,method:'post',
		success:function(transport) {
			var retObj = $.evalJSON(transport);
			if(retObj.processId=='1'){
	            var parentWindow = window.dialogArguments;
	         	hideParentRMenu(parentWindow);
				var newNode = {id:retObj.id, name:$$("po.deptCname").value,title:$$("po.deptName").value};
				if (parentWindow.zTree.getSelectedNodes()[0]) {
					parentWindow.zTree.addNodes(parentWindow.zTree.getSelectedNodes()[0], newNode);
				} else {
					parentWindow.zTree.addNodes(null, newNode);
				}
			}
			alert(retObj.msg);
			window.close();
		}
	});
}

/**
 * 删除目录
 */
function delDept()
{
	alert("部门不允许删除！");
}

/**
 * 跳转到更新目录页面
 */
function goUpdateDept()
{
	var id = "";
	if (zTree.getSelectedNodes()[0]) {
		id = zTree.getSelectedNodes()[0].id;
	}else{
		return;
	}
   
	var retValue = window.showModalDialog(getDeptAction()
			+ "?cmd=goUpdate&po.id=" + id,
			window, "menubar=no;toolbar=no");
}

/**
 * 更新一条记录
 */
function updateDept() {
	if(!checkForm())
	{
		return;
	}
	var url = getDeptAction()+"?"+getDeptForm().formSerialize();	
	new $.ajax({url:url,method:'post',
		success:function(transport) {
			var retObj = $.evalJSON(transport);
			if(retObj.processId=='1'){
	            var parentWindow = window.dialogArguments;
	         	hideParentRMenu(parentWindow);
	         	var sn = parentWindow.zTree.getSelectedNodes()[0];
				if (sn) {
					var newNode = {id:sn.id, name:$$("po.deptCname").value,title:$$("po.deptName").value};
					parentWindow.zTree.updateNode(sn,newNode);
				} 
			}
			alert(retObj.msg);
			window.close();
		}
	});
}


function saveOrUpdate(url,type){
	$.ajax({
		type: "POST",
		url: url,
		dataType:"json",
		success: function(returnedData){
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				var parentWindow = window.dialogArguments;
				parentWindow.list(1);
			}
			if(type !== 'addNext' )	//未定义
				window.close();
			getForm().get(0).reset();		//重置
		}
	});
	
}
